package collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
 * 对字符串集合排序
 */
public class SortString {
    public static void main(String[] args) {
        /*
        List<String> list = new ArrayList<>();
        list.add("jack");
        list.add("rose");
        list.add("tom");
        list.add("jerry");
        list.add("black");
        list.add("Kobe");
        System.out.println("list原始数据: "+list); //[jack, rose, tom, jerry, black, Kobe]
        //对英文字符串排序时，会按首字母的ASCII码来排
        //若首字母相同，则比较第2个字母的ASCII码，以此类推
        Collections.sort(list); //升序
        System.out.println("list排序后数据:"+list); //[Kobe, black, jack, jerry, rose, tom]
        */

        List<String> list = new ArrayList<>();
        list.add("王克晶");
        list.add("传奇老师很幽默");
        list.add("伟超老师");
        System.out.println("list原始数据: "+list); //[王克晶, 传奇老师很幽默, 伟超老师]

        //自定义排序规则:
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length()-o2.length(); //按照字符串长度升序排序
            }
        });
        Collections.sort(list,(o1,o2)->o1.length()-o2.length());
        System.out.println("list排序后数据:"+list);
    }
}















